/**
 * 利用apache ant工具类进行解压缩，克服java标准工具类兼容格式低的缺点
 */
package com.cnfangmao.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;


public class AntZipUtil {
	    private final static int BUFFER_LENGTH=1000;

	    //压缩文件夹内的文件 
	    public static boolean doZip(String zipDirectory,String strDestFileName)
	    {//zipDirectoryPath:需要压缩的文件夹名 
	    	boolean bResult=false;
	        File zipDir; 
	        ZipOutputStream zipOut; 
	        
	        zipDir = new File(zipDirectory); 
	      //  String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名 

	        try
	        { 
	            zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(strDestFileName))); 
	            handleDir(zipDir , zipOut); 
	            zipOut.close(); 
	            bResult=true;
	        }catch(IOException ioe){ 
	            ioe.printStackTrace(); 
	        } 
	        
	        return bResult;
	    } 

	    //由doZip调用,递归完成目录文件读取 
	    private static void handleDir(File dir , ZipOutputStream zipOut)throws IOException{ 
	        FileInputStream fileIn; 
	        File[] files; 
	        byte[] buf=new byte[BUFFER_LENGTH];
	        int  readedBytes=0; 
	        files = dir.listFiles(); 
	     
	        if(files.length == 0){//如果目录为空,则单独创建之. 
	            //ZipEntry的isDirectory()方法中,目录以"/"结尾. 
	            zipOut.putNextEntry(new ZipEntry(dir.toString() + "/")); 
	            zipOut.closeEntry(); 
	        } 
	        else{//如果目录不为空,则分别处理目录和文件. 
	            for(File fileName : files){ 
	                //System.out.println(fileName); 

	                if(fileName.isDirectory()){ 
	                    handleDir(fileName , zipOut); 
	                } 
	                else{ 
	                    fileIn = new FileInputStream(fileName); 
	                    zipOut.putNextEntry(new ZipEntry(fileName.toString())); 

	                    while((readedBytes = fileIn.read(buf))>0){ 
	                        zipOut.write(buf , 0 , readedBytes); 
	                    } 

	                    zipOut.closeEntry(); 
	                } 
	            } 
	        } 
	    } 

	    //解压指定zip文件 
	    public static boolean unZip(String unZipfileName,String strDestDir){//unZipfileName需要解压的zip文件名 
	    	boolean bResult=false;
	    	
	        FileOutputStream fileOut; 
	        File file; 
	        InputStream inputStream; 
	        byte[] buf=new byte[BUFFER_LENGTH];
	        int  readedBytes=0; 
	        
	        try{ 
	            ZipFile zipFile = new ZipFile(unZipfileName); 

	            for(Enumeration entries = zipFile.getEntries(); entries.hasMoreElements();){ 
	                ZipEntry entry = (ZipEntry)entries.nextElement(); 
	                file = new File(strDestDir+File.separatorChar+entry.getName()); 

	                if(entry.isDirectory()){ 
	                    file.mkdirs(); 
	                } 
	                else{ 
	                    //如果指定文件的目录不存在,则创建之. 
	                    File parent = file.getParentFile(); 
	                    if(!parent.exists()){ 
	                        parent.mkdirs(); 
	                    } 

	                    inputStream = zipFile.getInputStream(entry); 

	                    fileOut = new FileOutputStream(file); 
	                    while(( readedBytes = inputStream.read(buf) ) > 0){ 
	                        fileOut.write(buf , 0 , readedBytes ); 
	                    } 
	                    fileOut.close(); 

	                    inputStream.close(); 
	                }    
	            } 
	            zipFile.close(); 
	            bResult=false;
	        }catch(IOException ioe){ 
	            ioe.printStackTrace(); 
	        } 
	        
	        return bResult;
	    } 
}
